using System;
using System.Collections.Generic;
using System.Text;

namespace ElteIk.RoboSoccer.UnitTests
{
    class RunnerThread
    {
        public enum Results
        {
            Timeout,            
            InvariantFailed,
            PreconditionFailed
        }

        private Core.RoboSoccerGame game;

        private bool evaluationResult;

        public RunnerThread()
        {
            game = new ElteIk.RoboSoccer.Core.RoboSoccerGame();

            game.StepLength = 0.2f;
            game.StepInterval = 0;

            game.Updated += new EventHandler(game_Updated);
            game.RoundStart += new EventHandler(game_RoundStart);

            game.Initialize();
        }

        public event EventHandler GameUpdated;
        protected void OnGameUpdated()
        {
            if (GameUpdated != null)
                GameUpdated(this, EventArgs.Empty);
        }

        void game_RoundStart(object sender, EventArgs e)
        {
        }

        void game_Updated(object sender, EventArgs e)
        {
            OnGameUpdated();
            evaluationResult = OnEvaluateExitCondition(this.game);
        }

        public delegate bool EvaluateExitConditionDelegate(Core.RoboSoccerGame game);

        public event EvaluateExitConditionDelegate EvaluateExitCondition;

        protected virtual bool OnEvaluateExitCondition(Core.RoboSoccerGame game)
        {
            if (EvaluateExitCondition != null)
                return EvaluateExitCondition(game);
            else
                return false;
        }

        public bool Start()
        {
            game.Start();
            Main();
            return evaluationResult;
        }

        public void Main()
        {
            while (true)
            {
                System.Threading.Thread.Sleep(10);
                if (evaluationResult || game.Time > 60)
                {
                    return;
                }
            }
        }
    }
}
